package br.com.pontoclass.main;

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class CartesianPlotter extends JPanel {

    private static final long serialVersionUID = -2990771940704659354L;

    // Data sampling
    double[] data = {39,31,15,-3,-17,-21,-9,25,87};

    // Define size of plotting panel
    static int W=600;
    static int H=400;

    public CartesianPlotter() {
        setPreferredSize(new java.awt.Dimension(W,H));
    }

    @Override public void paint(Graphics gr) {
        // horizontal scaling
        double xScale = W / data.length;   

        // vertical scaling
        double yMinimum = -21;  // should use a for loop to find it out
        double yMaximum = 87;   // should use a for loop to find it out
        double yScale = H / (yMaximum-yMinimum);

        // actually plotting the graph
        int xPrev=-1, yPrev=-1;
        for (int i=0;i<data.length;i++) {
            int x = (int) ( i*xScale );
            int y = (int) ( (data[i] - yMinimum) *yScale );
            y = H-y;  // up-side down
            plot(gr,x,y);
            // join data point with straight line
            if (xPrev!=-1) gr.drawLine(xPrev,yPrev,x,y);
            xPrev=x; yPrev=y;
        }
    }

    public void plot(Graphics gr, int x, int y) {
        // draw a small rectangle to represent the data point
        gr.setColor(java.awt.Color.RED);
        int width = 10;
        int height = 10;
        int xUpperLeft = x-width/2;
        int yUpperLeft = y-height/2;
        gr.fillRect(xUpperLeft, yUpperLeft, width, height);
    }

    public static void main(String[] args) throws Exception {
        JFrame frame=new JFrame("Plotter");
        frame.add(new CartesianPlotter());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
